<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}菜单`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="菜单标题" prop="title">
        <el-input v-model="drawerProps.row!.title" clearable></el-input>
      </el-form-item>
      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="drawerProps.row!.name" clearable></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon">
        <SelectIcon v-model:icon-value="drawerProps.row!.meta!.icon" />
      </el-form-item>
      <el-form-item label="路由地址" prop="path">
        <el-input v-model="drawerProps.row!.path" clearable></el-input>
      </el-form-item>
      <el-form-item label="组件路径" prop="component">
        <el-input v-model="drawerProps.row!.component" clearable></el-input>
      </el-form-item>
      <el-form-item label="外部链接" prop="isLink">
        <el-input v-model="drawerProps.row!.meta!.isLink" clearable></el-input>
      </el-form-item>
      <el-form-item label="默认加载" prop="isAffix">
        <common-select
          data-key="IS_NOT"
          :request-api="() => apiService.findClassify('IS_NOT')"
          v-model="drawerProps.row!.meta!.isAffix"
        ></common-select>
      </el-form-item>
      <el-form-item label="父菜单" prop="pid">
        <cascader-select
          data-key="menu"
          :request-api="getMenuList"
          :config="{ label: 'title', value: '_id', checkStrictly: true }"
          v-model:item-value="drawerProps.row!.pid"
        ></cascader-select>
      </el-form-item>
      <el-form-item label="序号" prop="sort">
        <el-input v-model="drawerProps.row!.sort" clearable></el-input>
      </el-form-item>
      <el-form-item label="公开" prop="public">
        <common-select
          data-key="IS_NOT"
          :request-api="() => apiService.findClassify('IS_NOT')"
          v-model="drawerProps.row!.public"
        ></common-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import SelectIcon from "@/components/SelectIcon/index.vue";
import cascaderSelect from "@/components/SelectFilter/cascader-select.vue";
import apiService from "@/api/services/apiService";
import commonSelect from "@/components/SelectFilter/common-select.vue";

const rules = reactive({
  name: [{ required: true, message: "请输入组织名" }]
});

interface DrawerProps {
  title: string;
  tbName: string;
  isView: boolean;
  row: Partial<Menu.MenuOptions>;
  api?: (tabName: string, params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  tbName: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      if (drawerProps.value.row.title) {
        drawerProps.value.row.meta!.title = drawerProps.value.row.title;
      }
      await drawerProps.value.api!(drawerProps.value.tbName, drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

const getMenuList = () => {
  return apiService.findTree("menus");
};

defineExpose({
  acceptParams
});
</script>
@/api/types
